/*===========================================================================
project:      nc_hiring_credits
Author:       Jorge Perez, Michael Suher
Program Name: logemp_rdcellini_wave1_tier1_vs_2.do
Dependencies: Brown, NYU
---------------------------------------------------------------------------
Creation Date:      Oct 25, 2016
Modification Date:    
version:            1  
References:     
---------------------------------------------------------------------------
Regression discontinuity regressions
For unemployment
Tier 1 vs Tier 2

---------------------------------------------------------------------------
Change log:

8/16/2018	Stop using rankings before 1996, use rankings in 2000 again because they've been
			rebuilt and match 
			Start sample in 1996. CFR ITT sample starts in 1993, 
			which is at most 2 yrs before 1st election (1995)
			However for TOT estimates Cellini starts at T=0
			Include all counties that are ever among the 50 most distressed, but only use the
			50 that correspond to each lag to estimate each coefficient. This is analogous to
			the CFR code too, where 50 counties enter each focal election.
			
			
				
	
===========================================================================*/


/*=========================================================================
                        Project calls
===========================================================================*/

version 14
cap project, doinfo
if _rc==198 { 
 if c(username)=="J16339" loc master = "T:/NorthCarolinaHiringCredits"
	else if c(username)=="jorpp" loc master "C:/Users/jorpp/Dropbox (Brown)/NorthCarolinaHiringCredits"
	else loc master "U:/M1MXS14/NorthCarolinaHiringCredits"
	loc pr=0
	cap log close
	log using "`master'/code/est/logemp_rdcellini_wave1_tier1_vs_2.log", text replace	
}
else {
	loc master "`r(pdir)'"
	loc doname "`r(dofile)'"
	loc pr=1
	project, uses("`master'/Data/source/diffw1t1v2.dta")
	project, relies_on("`master'/code/est/build_defiers.do")
	* project, relies_on("`master'/code/est/diff_settings.do")
}

/*=========================================================================
                        Required commands
===========================================================================*/
/*
cap noi  ssc install estout
cap noi  ssc install reghdfe
cap noi  ssc install coefplot
cap noi  ssc install clustse
cap noi ssc install tsspell
*/

/*=========================================================================
                        0: Define programs
===========================================================================*/

*------------------------ 0.1: Program to get predictions ----------------------------------

cap program drop getprediction
program define getprediction

	* Prediction turning off all the treatments

	predict log_emp_hat_`1' if inrange(year,1996,2006) & insample	
	cap drop `2'_actual
	gen `2'_actual = `2'
	replace `2' = 0
	predict log_emp_hat_`1'_no_0123 if inrange(year,1996,2006) & insample	

	* Prediction leaving 3, turning off 2, 1 and 0

	replace `2' = `2'_actual

	mat bb = e(b)
	mat bb[1,1]=0
	mat bb[1,2]=0
	mat bb[1,3]=0
	ereturn post bb


	predict log_emp_hat_`1'_no_012 if inrange(year,1996,2006) & insample	


	/* br cty year log_emp log_emp_hat log_emp_hat_0 `2'_actual */

	gen emp_hat_`1'_no_0123 = exp(log_emp_hat_`1'_no_0123)
	gen emp_hat_`1'_no_012 = exp(log_emp_hat_`1'_no_012)
	replace emp_hat_`1'_no_0123 = emp if emp_hat_`1'_no_0123 == .
	replace emp_hat_`1'_no_012 = emp if emp_hat_`1'_no_012 == .

end 


/*=========================================================================
                        1: Import data and set up variables
===========================================================================*/

use "`master'/Data/source/diffw1t1v2.dta", clear

* rd settings, these are very heterogenous so keep them herre instead of separate file

glo lags=3
glo cutoff1=10
glo cutoff2=50

gen running=distress_rank-$cutoff1
gen intent = running<=0

* I'm not using powers now, but it's safe to run these linears first b/c we need them anyway. Do graphs afterwards, then come back to the powers issue. Remember that KC wanted linears anyway

drop if cty==.
sort cty year
xtset cty year

forv l=1(1)3 {
	cap drop l`l'running
	gen l`l'running = l`l'.running	
}

* Treatment
gen rdtreat= (grouped_tier==1)
gen rdtreat2 = (grouped_tier==2)
gen rdtreat3 = (grouped_tier==3)
do "`master'/code/est/build_defiers.do"

gen yr2000= (year==2000)
gen post = inrange(year,1996,2006)
gen gt1996 = inrange(year,1997,2006)
gen gt1997 = inrange(year,1998,2006)
gen gt1998 = inrange(year,1999,2006)

gen m0=distress_rank<=50 & post
gen m1=l1.distress_rank<=50 & gt1996
gen m2=l2.distress_rank<=50 & gt1997
gen m3=l3.distress_rank<=50 & gt1998

gen wave0 = (wave0_treat_1996 & year==1996)
gen l1wave0 = (wave0_treat_1996 & year==1997)
gen l2wave0 = (wave0_treat_1996 & year==1998)
gen l3wave0 = (wave0_treat_1996 & year==1999)

* Outcome
glo outcome "log_emp"

est clear

* Choose sample
* Among the 50 most distressed counties in any year between 96 and 2006
gen inrank= (distress_rank<=50) & inrange(year,1996,2006)
replace inrank=0 if inrank==.
bys cty (year): egen insample=max(inrank)

* To streamline, intent, running, distress_rank and defier should be 0 in preperiods

foreach x in intent running distress_rank defier rdtreat {
	replace `x'=0 if year<1996
}

gen d0m01 = 0.defier#1.m0
gen ld0m11 = 0.l1.defier#1.m1
gen l2d0m21 = 0.l2.defier#1.m2
gen l3d0m31 = 0.l3.defier#1.m3

gen d1m01 = 1.defier#1.m0
gen ld1m11 = 1.l1.defier#1.m1
gen l2d1m21 = 1.l2.defier#1.m2
gen l3d1m31 = 1.l3.defier#1.m3


/*=========================================================================
                        2: Get prediction from OLS estimates
===========================================================================*/

*------------------------ 2.1: OLS without controls ----------------------------------

est use "`master'/ster2/rdcellini_e_w1_t12_c13.ster"

getprediction c13 intent

*------------------------ 2.2: OLS with controls ----------------------------------

est use "`master'/ster2/rdcellini_e_w1_t12_c13_c.ster"

getprediction c13_c intent

/*=========================================================================
                        3: Get prediction from IV estimates
===========================================================================*/

* IVs have to be reestimated without the -partial- option

set matsize 1000


*------------------------ 3.1: IV without controls ----------------------------------

# delimit ;
	ivreg2 ${outcome} 
	i.cty
	(1.rdtreat#1.m0 1.l1.rdtreat#1.m1 1.l2.rdtreat#1.m2 1.l3.rdtreat#1.m3 = 
	1.intent#1.m0 1.l1.intent#1.m1 1.l2.intent#1.m2 1.l3.intent#1.m3 )
	c.running#1.m0 c.l1.running#1.m1 c.l2.running#1.m2 c.l3.running#1.m3 
	
	i.year
	i.year#1.m0
		
	wave0 l1wave0 l2wave0 l3wave0
	
	1.m0 1.m1 1.m2 1.m3
	
	l(4/6).rdtreat
	l(4/6).rdtreat2	
	l(4/6).rdtreat3
	
	if inrange(year,1996,2006)
	& insample	
	, cluster(cty) 
	first ;
# delimit cr

* Margin
margins l3.rdtreat#m3

getprediction c13iv rdtreat


*------------------------ 3.2: IV with controls ----------------------------------

# delimit ;
	ivreg2 ${outcome} 
	i.cty
	(1.rdtreat#1.m0 1.l1.rdtreat#1.m1 1.l2.rdtreat#1.m2 1.l3.rdtreat#1.m3 = 
	1.intent#1.m0 1.l1.intent#1.m1 1.l2.intent#1.m2 1.l3.intent#1.m3 )
	c.running#1.m0 c.l1.running#1.m1 c.l2.running#1.m2 c.l3.running#1.m3 
	
	i.year
	i.year#1.m0
		
	wave0 l1wave0 l2wave0 l3wave0
	
	1.m0 1.m1 1.m2 1.m3
	
	l(4/6).rdtreat
	l(4/6).rdtreat2	
	l(4/6).rdtreat3
	
	l3.(unemp_rate_3 popgrow_est_cen inc_pc_3yr_avg)
	if inrange(year,1996,2006)
	& insample	
	, cluster(cty)
	first ;
# delimit cr

* Margin
margins l3.rdtreat#m3

getprediction c13iv_c rdtreat

/*=========================================================================
                        4: Make graphs of extra employment and "hires"
===========================================================================*/

collapse (sum) emp_hat_* , by(year)

graph drop _all

foreach x in c13 c13_c c13iv c13iv_c {

	if "`x'" == "c13" loc title "OLS - No Controls"
	if "`x'" == "c13_c" loc title "OLS - Controls"
	if "`x'" == "c13iv" loc title "IV - No Controls"
	if "`x'" == "c13iv_c" loc title "IV - Controls"

	tsset year
	gen empdiff`x' = emp_hat_`x'_no_012 - emp_hat_`x'_no_0123
	sum d.empdiff`x' if inrange(year,1996,2006)
	loc shires : di %9.0f r(sum)
	line empdiff`x' year if inrange(year,1996,2006), name(g`x') title(`title') subtitle(`" "Extra Hires" = `shires'"') /* " */
}

